package com.minyifang.wcspf.work.ui.user.presenter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.minyifang.wcspf.common.base.BasePresenter;
import com.minyifang.wcspf.common.config.network.Constants;
import com.minyifang.wcspf.common.config.network.RetrofitHelper;
import com.minyifang.wcspf.common.config.user.UserManager;
import com.minyifang.wcspf.common.md5.SafeUtils;
import com.minyifang.wcspf.common.utils.LogUtil;
import com.minyifang.wcspf.work.ui.user.model.OrderBean;
import com.minyifang.wcspf.work.ui.user.view.OrderListView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class OrderListPresenter extends BasePresenter<OrderListView> {
    public void getOrderList(String type, int pageIndex) {
        Map<String, Object> map = new HashMap<>();
        map.put("status", type);
        map.put("page", pageIndex);
        map.put("size", 20);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getOrderList(requestBody), respond -> {
            if (respond.result == 1) {
                LogUtil.e("我的订单数据 ：" + SafeUtils.decrypt(respond.data));
                List<OrderBean> list = new Gson().fromJson(SafeUtils.decrypt(respond.data), new TypeToken<List<OrderBean>>() {
                }.getType());
                getView().showOrderList(list);
            } else
                getView().onNoOrderList();
        });
    }

    /**
     * 取消订单
     *
     * @param id
     */
    public void cancelOrder(String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", id);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().cancelOrder(requestBody), respond -> {
            getView().hideLoadingView();
            if (respond.result == 1) {

                getView().showCancelOrderRes(respond.message);
            } else
                getView().showRequestFailed(respond.message);
        });
    }

    /**
     * 确认收货取货
     *
     * @param id
     */
    public void confirmOrder(String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", id);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().confirmOrder(requestBody), respond -> {
            getView().hideLoadingView();
            if (respond.result == 1) {
                getView().showConfirmOrderRes(respond.message);
            } else
                getView().showRequestFailed(respond.message);
        });
    }

    public void buyAgain(String orderId) {
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().buyAgain(requestBody),respond->{
            if (respond.result == 1) {
                getView().showBuyAgainRes(respond.message);
            } else
                getView().showRequestFailed(respond.message);
        });
    }
}